import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';
import { Https } from '../../app/http.service';
/**
 * Generated class for the DailiPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-daili',
  templateUrl: 'daili.html',
})
export class DailiPage {
	Url = new Https().Url + "act=goods&op=app_daili";
	constructor(public navCtrl: NavController, public navParams: NavParams, public toastCtrl: ToastController) {
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad DailiPage');
	}

	submit(int){
		
		if (int.length == 6){
			let id = int.replace(/\b(0+)/gi, "")
			fetch(this.Url,{
				method: "post",
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token") + "&id=" + id,
			}).then((response) => {
				response.text().then((data)=>{
					if(data == '1'){
						this.toast("绑定成功");
						this.navCtrl.pop();
					}else{
						this.toast("绑定失败");
					}
				})
				
			}).catch((err)=>{
				this.toast("出错：" + err);
			})
		}else{
			this.toast('号码有误，请重新输入');
		}
	}
	toast(text){
		let toast = this.toastCtrl.create({
			message: text,
			duration: 3000
		});
		toast.present();
	}
}
